package com.s59.rabc.controller;

import com.s59.rabc.commen.Dta;
import com.s59.rabc.commen.RequiredPerms;
import com.s59.rabc.entity.User;
import com.s59.rabc.service.IUserService;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping
//    @RequiredPerms("user:add")
//    @RequiresPermissions("user:add")
    @PreAuthorize("hasAnyAuthority('user:add')")

    public Dta add(@RequestBody User role){
        userService.save(role);
        return Dta.ok();
    }

//    @RequiresRoles("manager")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('user:delete')")
//    @RequiredPerms("user:del")
    public Dta del(@PathVariable Long id){
        userService.removeById(id);
        return Dta.ok();
    }

//    @RequiresPermissions("user:update")
    @PutMapping
//    @RequiredPerms("user:update")
    @PreAuthorize("hasAnyAuthority('user:update')")
    public Dta update(@RequestBody User role){
        userService.saveOrUpdate(role);
        return Dta.ok();
    }

    @GetMapping
    @PreAuthorize("hasAnyAuthority('user:list')")
//    @RequiresPermissions("user:list")
//    @RequiredPerms("user:list")
    public Dta getAll(){
        List<User> list =userService.list();
        return Dta.ok(list);
    }
}
